﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Data;
using BaseDatos.Comun;
using BaseDatos;
using System.Reflection;
using System.Drawing;

namespace Sistema.Menu.SCHD
{
    public partial class MenuOP : MenuStrip
    {
        private DataSet DsMenu = null;
        public MenuOP(string Modulo, string Usuario)
        {
            InitializeComponent();
            CargarMenu(Modulo, Usuario);
        }
        public MenuOP(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        protected void CargarMenu(string Modulo, string Usuario)
        {
            try
            {
                this.Dock = DockStyle.Top;
              
                //MainForm.Controls.Add(tabControl1);
                DbParametro[] Parametros = new DbParametro[3];
                Parametros[0] = new DbParametro("plogin", Usuario);
                Parametros[1] = new DbParametro("pcodmod", Modulo);
                Parametros[2] = new DbParametro("popcion", "QRY02");
                DsMenu = ConexionBaseDatos.Conexion.CrearDataSet("GETCK_MODULOXOPCION", Parametros);

                if (DsMenu.Tables[0].Rows.Count > 0)
                {
                    ToolStripMenuItem LastPadMenu = new ToolStripMenuItem();
                    //LastPadMenu.Dock = DockStyle.Left;
                    
                    ToolStripMenuItem LastPopupMenu = new ToolStripMenuItem();
                    //LastPopupMenu.Dock = DockStyle.Left;
                    foreach (DataRow registro in DsMenu.Tables[0].Rows)
                    {
                        //PAD
                        if (registro["FlagPadPopup"].ToString().Trim() == "0")
                        {
                            ToolStripMenuItem NewPadMenu = new ToolStripMenuItem(registro["Opcion"].ToString().Trim());
                            LastPadMenu = NewPadMenu;
                            Items.Add(LastPadMenu);
                        }
                        //POPUP
                        if (registro["FlagPadPopup"].ToString().Trim() == "1") //&& registro["Orden"].ToString().Substring(4, 2) == "00"
                        {
                            ToolStripMenuItem NewPopupMenu = null;
                            if (registro["Comando"].ToString() == "")
                            {
                                NewPopupMenu = new ToolStripMenuItem(registro["Opcion"].ToString().Trim());
                            }
                            else
                            {
                                NewPopupMenu = new ToolStripMenuItem(registro["Opcion"].ToString().Trim(), null, new EventHandler(PopupItemMenu_Click));
                            }

                            NewPopupMenu.Enabled = registro["Acceso"].ToString().Trim() == "1";
                            NewPopupMenu.Tag = registro["Comando"].ToString().Trim();
                            LastPadMenu.DropDownItems.Add(NewPopupMenu);
                            LastPopupMenu = NewPopupMenu;
                            if (registro["FlagLinea"].ToString().Trim() == "1")
                            {
                                ToolStripSeparator NewSeparatorMenu = new ToolStripSeparator();
                                LastPadMenu.DropDownItems.Add(NewSeparatorMenu);
                            }
                        }

                        //POPUP - SUB
                        if (registro["FlagPadPopup"].ToString().Trim() == "2")
                        {
                            ToolStripMenuItem NewSubPopupMenu = new ToolStripMenuItem(registro["Opcion"].ToString().Trim(), null, new EventHandler(PopupItemMenu_Click));
                            NewSubPopupMenu.Enabled = registro["Acceso"].ToString().Trim() == "1";
                            NewSubPopupMenu.Tag = registro["Comando"].ToString().Trim();
                            LastPopupMenu.DropDownItems.Add(NewSubPopupMenu);
                            if (registro["FlagLinea"].ToString().Trim() == "1")
                            {
                                ToolStripSeparator NewSeparatorMenu = new ToolStripSeparator();
                                LastPopupMenu.DropDownItems.Add(NewSeparatorMenu);
                             
                                //LastPadMenu.DropDownItems.Add(NewSeparatorMenu);
                            }
                        }

                    }
                }

                
            }
            catch (Exception ex)
            {
                //throw;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        protected void PopupItemMenu_Click(Object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem Barra = (ToolStripMenuItem)sender;
                
                if (!string.IsNullOrEmpty(Barra.Tag.ToString()))
                {
                    String[] ArrayString = Barra.Tag.ToString().Split(';');
                    String FrmName = ArrayString[0];
                    String DllExeName = ArrayString[ArrayString.Length - 1];
                    if (FrmName.Trim().ToUpper() == "CLOSE()" || FrmName.Trim().ToUpper() == "ME.CLOSE()")
                    {
                        Form MainForm = (Form)this.Parent;
                        MainForm.Close();
                    }
                    else
                    {

                       
                        Assembly AssemblyReflection = Assembly.GetExecutingAssembly();
                        AssemblyReflection = Assembly.LoadFrom(DllExeName.Trim());
                        Type FrmType = AssemblyReflection.GetType(FrmName);
                        Object Obj = System.Activator.CreateInstance(FrmType);
                        Form Frm = (Form)Obj;
                        Form MainForm = (Form)this.Parent;
                        Frm.MdiParent = MainForm;
                        Frm.Show();
                        
                       // tab.Dock = System.Windows.Forms.DockStyle.Fill;
                        
                       // tab.Location = new System.Drawing.Point(0, 24);

                       // MainForm.Controls.Add(tab);
                      
                      //  TabPage page = new TabPage("IContainer Example"); //create our IContainer/TabPage

                       // Button button = new Button(); //create our button
                       // button.Text = "Hello Button!"; //set our buttons text
                       // button.Dock = DockStyle.Fill; //make our button fill up the tab page when added

                       //// button.Click += new EventHandler(button_Click); //this adds the below button_Click event
                       // page.Controls.Add(button); //add our button to the tab page
                       // tab.TabPages.Add(page); //add our tab page to the tab control
                       // tab.SelectedTab = page;


                        //tab.TabPages.Add("ABC");
                        //MainMenu 

                       // Frm.MdiParent = MainForm;
                       // tabControl1.Dock = DockStyle.Bottom;
                        
                        
                        //MainForm.Controls.Add(tabControl1);
                        
                       // tab.TabPages.Add("Equipo");
 

                        //----
                        //this.tabControl1 = new TabControl();
                        //Form Main = (Form)this.Parent;
                        //this.tabPage1 = new TabPage();

                        // Gets the controls collection for tabControl1. 
                        // Adds the tabPage1 to this collection. 
                        
                        //Add a Tabpage and enables it
                       // TabPage tp = new TabPage();
                       //// tp.Parent = tabControl1;
                       // tp.Text = Frm.Text;
                       //// tp.Controls.Add(Frm);
                       // tp.Show();
                       //// Frm.Show();

                       // this.tab.TabPages.Add(tp);
                        //child Form will now hold a reference value to a tabpage
                        
                        //Frm.TabPag = tp;
                        //this.tabControl1.Location = new Point(25, 25);
                        //this.tabControl1.Size = new Size(250, 250);
                       // Main.Controls.Add(tabControl1);
                    }
                }
            }
            catch (Exception ex)
            {
                //throw;
                MessageBox.Show(ex.InnerException.Message);
            }
        }
    }
}
